<!DOCTYPE html>
<html lang="en">
  <head>
    <title>_KeyValueCodingAndObserving Extension Reference</title>
    <link rel="stylesheet" type="text/css" href="../css/jazzy.css" />
    <link rel="stylesheet" type="text/css" href="../css/highlight.css" />
    <meta charset='utf-8'>
    <script src="../js/jquery.min.js" defer></script>
    <script src="../js/jazzy.js" defer></script>
    
  </head>
  <body>
    <a name="//apple_ref/swift/Extension/_KeyValueCodingAndObserving" class="dashAnchor"></a>
    <a title="_KeyValueCodingAndObserving Extension Reference"></a>
    <header>
      <div class="content-wrapper">
        <p><a href="../index.html">Closures Docs</a></p>
        <p class="header-right"><a href="https://github.com/vhesener/Closures"><img src="../img/gh.png"/>View on GitHub</a></p>
      </div>
    </header>
    <div class="content-wrapper">
      <p id="breadcrumbs">
        <a href="../index.html">Closures Reference</a>
        <img id="carat" src="../img/carat.png" />
        _KeyValueCodingAndObserving Extension Reference
      </p>
    </div>
    <div class="content-wrapper">
      <nav class="sidebar">
        <ul class="nav-groups">
          <li class="nav-group-name">
            <a href="../Controls.html">Controls</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="../Extensions/UIButton.html">UIButton</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UISwitch.html">UISwitch</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UITextField.html">UITextField</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIDatePicker.html">UIDatePicker</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIPageControl.html">UIPageControl</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UISegmentedControl.html">UISegmentedControl</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIStepper.html">UIStepper</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UISlider.html">UISlider</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIControl.html">UIControl</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIBarButtonItem.html">UIBarButtonItem</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="../Scrolling Views.html">Scrolling Views</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="../Extensions/UITableView.html">UITableView</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UICollectionView.html">UICollectionView</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIPickerView.html">UIPickerView</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIScrollView.html">UIScrollView</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="../Gesture Recognizers.html">Gesture Recognizers</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="../Extensions/UIGestureRecognizer.html">UIGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UITapGestureRecognizer.html">UITapGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UILongPressGestureRecognizer.html">UILongPressGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIPinchGestureRecognizer.html">UIPinchGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIPanGestureRecognizer.html">UIPanGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UISwipeGestureRecognizer.html">UISwipeGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIRotationGestureRecognizer.html">UIRotationGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIScreenEdgePanGestureRecognizer.html">UIScreenEdgePanGestureRecognizer</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIView.html">UIView</a>
              </li>
              <li class="nav-group-task">
                <a href="../Gesture Recognizers.html#/s:8Closures9configure7gesture7handleryx_yxctSo19UIGestureRecognizerCRbzlF">configure(gesture:handler:)</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="../Controllers.html">Controllers</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="../Extensions/UIImagePickerController.html">UIImagePickerController</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIImagePickerController/MediaFilter.html">– MediaFilter</a>
              </li>
              <li class="nav-group-task">
                <a href="../Extensions/UIImagePickerController/Result.html">– Result</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="../KVO.html">KVO</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="../Extensions/_5FKeyValueCodingAndObserving.html">_KeyValueCodingAndObserving</a>
              </li>
            </ul>
          </li>
        </ul>
      </nav>
      <article class="main-content">
        <section>
          <section class="section">
            <h1>_KeyValueCodingAndObserving</h1>
              <div class="declaration">
                <div class="language">
                  <pre class="highlight swift"><code><span class="kd">extension</span> <span class="n">_KeyValueCodingAndObserving</span></code></pre>

                </div>
              </div>
            
          </section>
          <section class="section task-group-section">
            <div class="task-group">
              <ul>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:10Foundation27_KeyValueCodingAndObservingP8ClosuresE7observe_7options5until13changeHandlerAA05NSKeyC11ObservationCs0B4PathCyxqd__G_So0mcF7OptionsVSbxcyx_AA0mC14ObservedChangeVyqd__GtctlF"></a>
                    <a name="//apple_ref/swift/Method/observe(_:options:until:changeHandler:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:10Foundation27_KeyValueCodingAndObservingP8ClosuresE7observe_7options5until13changeHandlerAA05NSKeyC11ObservationCs0B4PathCyxqd__G_So0mcF7OptionsVSbxcyx_AA0mC14ObservedChangeVyqd__GtctlF">observe(_:options:until:changeHandler:)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>This convenience method puts only a tiny bit of polish on
Swift&rsquo;s latest closure-based KVO implementation. Although
there aren&rsquo;t many obvious differences between this
method and the one in <code>Foundation</code>, there are a few helpers, which
are describe below.</p>

<p>First, it passes a remove condition, <code>until</code>, which is simpy a closure
that gets called to determine whether to remove the observation
closure. Returning true will remove the observer, otherwise, the
observing will continue. <code>Foundation</code>&lsquo;s method
automatically removes observation when the <code>NSKeyValueObservation</code>
is released, but this requires you to save it somewhere in your
view controller as a property, thereby cluttering up your view
controller with plumbing-only members.</p>

<p>Second, this method attempts to slightly improve the clutter. You
do not have to save the observer. <code>@discardableResult</code> allows you
to disregard the returned object entirely.</p>

<p>Finally, a typical pattern is to remove observers when deinit is
called on the object that owns the observed object.
For instance, if you are observing a model object property on your
view controller, you will probably want to stop observing when the
view controller gets released from memory. Because this is a
common pattern, there&rsquo;s a convenient var available on all subclasses
of NSObject named <code>selfDeinits</code>. Simply pass this as a parameter
into the <code>until</code> paramaeter, and the observation will be removed
when <code>self</code> is deallocated.</p>

<hr>
<h4 id='an-example-of-calling-this-method' class='heading'>An example of calling this method:</h4>
<pre class="highlight swift"><code><span class="o">&lt;</span><span class="err">#</span><span class="n">someObject</span><span class="err">#</span><span class="o">&gt;.</span><span class="nf">observe</span><span class="p">(\</span><span class="o">.&lt;</span><span class="err">#</span><span class="n">some</span><span class="o">.</span><span class="n">key</span><span class="o">.</span><span class="n">path</span><span class="err">#</span><span class="o">&gt;</span><span class="p">,</span> <span class="nv">until</span><span class="p">:</span> <span class="n">selfDeinits</span><span class="p">)</span> <span class="p">{</span> <span class="n">obj</span><span class="p">,</span><span class="n">change</span> <span class="k">in</span>
    <span class="o">&lt;</span><span class="err">#</span><span class="k">do</span> <span class="n">something</span><span class="err">#</span><span class="o">&gt;</span>
<span class="p">}</span>
</code></pre>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">@discardableResult</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="n">observe</span><span class="o">&lt;</span><span class="kt">Value</span><span class="o">&gt;</span><span class="p">(</span>
    <span class="n">_</span> <span class="nv">keyPath</span><span class="p">:</span> <span class="kt">KeyPath</span><span class="o">&lt;</span><span class="k">Self</span><span class="p">,</span> <span class="kt">Value</span><span class="o">&gt;</span><span class="p">,</span>
    <span class="nv">options</span><span class="p">:</span> <span class="kt">NSKeyValueObservingOptions</span> <span class="o">=</span> <span class="p">[],</span>
    <span class="n">until</span> <span class="nv">removeCondition</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="k">Self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Bool</span><span class="p">,</span>
    <span class="nv">changeHandler</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="k">Self</span><span class="p">,</span> <span class="kt">NSKeyValueObservedChange</span><span class="o">&lt;</span><span class="kt">Value</span><span class="o">&gt;</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)</span>
    <span class="o">-&gt;</span> <span class="kt">NSKeyValueObservation</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>keyPath</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The keyPath you wish to observe on this object</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>options</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The observing options</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>until</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The closure called when this handler should stop
observing. Return true if you want to forcefully stop observing.</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>changeHandler</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The callback that will be called when
the keyPath change has occurred.</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                      <div>
                        <h4>Return Value</h4>
                        <p>The observer object needed to remove observation</p>
                      </div>
                    </section>
                  </div>
                </li>
              </ul>
            </div>
          </section>
        </section>
        <section id="footer">
          <p>2017 Vincent Hesener</p>
          <p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.10.0</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
        </section>
      </article>
    </div>
  </body>
</div>
</html>
